<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登记日期" prop="plasmaDate">
        <el-date-picker clearable
          v-model="queryParams.plasmaDate"
          type="date"
          value-format="yyyyMMdd"
          placeholder="请选择登记日期">
        </el-date-picker>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10">
      <el-col :span="12">
        <el-row :gutter="10" class="mb8">
          <el-col :span="24" style="text-align: center;">宣传员关系（{{this.xuanchuanList.length}}）</el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="xuanchuanList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="流水号" width="60" align="center" prop="processno" />
          <el-table-column label="浆员姓名" align="center" prop="donorname" />
          <el-table-column label="是否采浆" align="center" prop="plasmacollection" >
            <template slot-scope="scope">
              <span>{{ scope.row.plasmacollection == "N" ? "" : "" }}</span>
              <span>{{ scope.row.plasmacollection == "Y" ? "已采浆" : "" }}</span>
              <span>{{ scope.row.plasmacollection == "I" ? "脂浆" : "" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="跟车" align="center" prop="chezhuname" />
        </el-table>
      </el-col>
      <el-col :span="12">
        <el-row :gutter="10" class="mb8">
          <el-col :span="24" style="text-align: center;">车主关系（{{this.chezhuList.length}}）</el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="chezhuList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50"  align="center" />
          <el-table-column label="流水号" width="60" align="center" prop="processno" />
          <el-table-column label="浆员姓名" align="center" prop="donorname" />
          <el-table-column label="是否采浆" align="center" prop="plasmacollection" >
            <template slot-scope="scope">
              <span>{{ scope.row.plasmacollection == "N" ? "" : "" }}</span>
              <span>{{ scope.row.plasmacollection == "Y" ? "已采浆" : "" }}</span>
              <span>{{ scope.row.plasmacollection == "I" ? "脂浆" : "" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="宣传员" align="center" prop="xuanchuanyuanname" />
        </el-table>
      </el-col>
    </el-row>
    

    
    

    <!-- 添加或修改浆员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="浆员编号" prop="donorNo">
          <el-input v-model="form.donorNo" placeholder="请输入浆员编号" />
        </el-form-item>
        <el-form-item label="浆员姓名" prop="donorName">
          <el-input v-model="form.donorName" placeholder="请输入浆员姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="建档日期" prop="issuingDate">
          <el-date-picker clearable
            v-model="form.issuingDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择建档日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="浆员状态" prop="codeDonorState">
          <el-input v-model="form.codeDonorState" placeholder="请输入浆员状态" />
        </el-form-item>
        <el-form-item label="plasmaDate" prop="plasmaDate">
          <el-date-picker clearable
            v-model="form.plasmaDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择plasmaDate">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDonor, getDonor, delDonor, addDonor, updateDonor } from "@/api/plasma/donor";
import { formatDate} from "@/api/plasma";
export default {
  name: "Donor",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宣传员关系表格数据
      xuanchuanList: [],
      // 车主关系表格数据
      chezhuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeIssuingDate: [],
      // 备注时间范围
      daterangePlasmaDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        donorNo: null,
        donorName: null,
        idCard: null,
        issuingDate: null,
        codeDonorState: null,
        plasmaDate: null,
        remark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {

    //this.daterangePlasmaDate = [formatDate(new Date(), 'yyyyMMdd'),formatDate(new Date(), 'yyyyMMdd')]
    this.queryParams.plasmaDate = formatDate(new Date(), 'yyyyMMdd');
    console.log(this.daterangePlasmaDate);
    this.getList();
  },
  methods: {
    /** 查询浆员管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeIssuingDate && '' != this.daterangeIssuingDate) {
        this.queryParams.params["beginIssuingDate"] = this.daterangeIssuingDate[0];
        this.queryParams.params["endIssuingDate"] = this.daterangeIssuingDate[1];
      }
      if (null != this.daterangePlasmaDate && '' != this.daterangePlasmaDate) {
        this.queryParams.params["beginPlasmaDate"] = this.daterangePlasmaDate[0];
        this.queryParams.params["endPlasmaDate"] = this.daterangePlasmaDate[1];
      }
      listDonor(this.queryParams).then(response => {
        console.log(response)
        this.xuanchuanList = response.data.xuanchuanList;
        this.chezhuList = response.data.chezhuList;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        donorNo: null,
        donorName: null,
        idCard: null,
        issuingDate: null,
        codeDonorState: null,
        plasmaDate: null,
        remark: null,
      };
      //this.daterangePlasmaDate = [formatDate(new Date(), 'yyyyMMdd'),formatDate(new Date(), 'yyyyMMdd')]
      this.queryParams.plasmaDate = formatDate(new Date(), 'yyyyMMdd');
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeIssuingDate = [];
      this.daterangePlasmaDate = [formatDate(new Date(), 'yyyyMMdd'),formatDate(new Date(), 'yyyyMMdd')]
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.donorId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加浆员管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const donorId = row.donorId || this.ids
      getDonor(donorId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改浆员管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.donorId != null) {
            updateDonor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDonor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const donorIds = row.donorId || this.ids;
      this.$modal.confirm('是否确认删除浆员管理编号为"' + donorIds + '"的数据项？').then(function() {
        return delDonor(donorIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('plasma/donor/export', {
        ...this.queryParams
      }, `donor_${new Date().getTime()}.xlsx`)
    },
    formatDate: function(value,args) {
        var dt = new Date(value);
        if(args == 'yyyy-M-d') {// yyyy-M-d
            let year = dt.getFullYear();
            let month = dt.getMonth() + 1;
            let date = dt.getDate();
            return `${year}-${month}-${date}`;
        } else if(args == 'yyyy-M-d H:m:s'){// yyyy-M-d H:m:s
            let year = dt.getFullYear();
            let month = dt.getMonth() + 1;
            let date = dt.getDate();
            let hour = dt.getHours();
            let minute = dt.getMinutes();
            let second = dt.getSeconds();
            return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
        } else if(args == 'yyyy-MM-dd') {// yyyy-MM-dd
            let year = dt.getFullYear();
            let month = (dt.getMonth() + 1).toString().padStart(2,'0');
            let date = dt.getDate().toString().padStart(2,'0');
            return `${year}-${month}-${date}`;
        } else if(args == 'yyyyMMdd') {// yyyy-MM-dd
            let year = dt.getFullYear();
            let month = (dt.getMonth() + 1).toString().padStart(2,'0');
            let date = dt.getDate().toString().padStart(2,'0');
            return `${year}${month}${date}`;
        } else {// yyyy-MM-dd HH:mm:ss
            let year = dt.getFullYear();
            let month = (dt.getMonth() + 1).toString().padStart(2,'0');
            let date = dt.getDate().toString().padStart(2,'0');
            let hour = dt.getHours().toString().padStart(2,'0');
            let minute = dt.getMinutes().toString().padStart(2,'0');
            let second = dt.getSeconds().toString().padStart(2,'0');
            return `${year}-${month}-${date} ${hour}:${minute}:${second}`;
        }
      }
  }
};
</script>
